﻿using GalaSoft.MvvmLight.Command;
using JiveMessenger.Common;
using JiveMessenger.Models;
using JiveMessenger.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556

namespace JiveMessenger.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MessageDownloadPage : BasicPage
    {
        public MessageDownloadPage()
        {

            this.InitializeComponent();

            // ApplicationView.GetForCurrentView().SetDesiredBoundsMode(ApplicationViewBoundsMode.UseCoreWindow);
            //var sb = StatusBar.GetForCurrentView();
            //sb.BackgroundOpacity = 1;
            //var foo = (Color)Application.Current.Resources["SystemColorControlAccentColor"];
            //sb.BackgroundColor = foo;

        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.
        /// This parameter is typically used to configure the page.</param>
        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            MessageDownloadParameter parameter = e.Parameter as MessageDownloadParameter;
            ViewModelMessageDownloadPage vm = (ViewModelMessageDownloadPage)this.DataContext;
            vm.ChatID = parameter.ChatID;
            vm.CountLimit = parameter.CountLimit;
            vm.AgeLimit = parameter.AgeLimit;
            vm.ChatName = parameter.ChatName;
            vm.ChatType = parameter.ChatType;
            this.TextBoxPageHeader.Opacity = 0;

            StatusBar.GetForCurrentView().HideAsync();

            if (e.NavigationMode == NavigationMode.New)
            {
                await Task.Delay(800);
                Storyboard sb = this.Resources["ShowPageHeaderAnim"] as Storyboard;
                sb.Begin();
            }
            else
                this.TextBoxPageHeader.Opacity = 1;

        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
        }

        protected override void SaveState(SaveStateEventArgs e)
        {
            base.SaveState(e);
        }
        protected override void LoadState(LoadStateEventArgs e)
        {
            base.LoadState(e);
        }
        private void BasicPage_Loaded(object sender, RoutedEventArgs e)
        {
            Window.Current.Activated += Current_Activated;
        }

        private void BasicPage_Unloaded(object sender, RoutedEventArgs e)
        {
            Window.Current.Activated -= Current_Activated;
        }

        async void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
                this.TextBoxPageHeader.Opacity = 0;
            else
            {
                await StatusBar.GetForCurrentView().HideAsync();
                await Task.Delay(800);
                Storyboard sb = this.Resources["ShowPageHeaderAnim"] as Storyboard;
                sb.Begin();
            }
        }
    }
}
